package net.easyjava.test.huangmeixi;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import net.easyjava.third.db.mysql.MysqlClient;
import net.easyjava.tools.http.HttpTools;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author ludi
 * @Date 2016 06 29 下午6:26
 */
public class GetData {
	public static final String  jdbcUrl = "jdbc:mysql://mingjuer.mysql.rds.aliyuncs.com:3306/crawler";
	public static final String  jdbcName = "juer";
	public static final String  jdbcPassword = "Juer2016";

	//获取所有文章列表 pageSize=185 所有数据 参数一个都不能删,报错
	public static final String URL_ROOT = "http://sdk.mobcent.com/baikesdk/info/getInfoList.do?boardId=11689399&page=1&pageSize=185&forumKey=9y8jrUJoJQj5vwip4X";
	//根据ID获取文章详情 参数不能删
	public static final String URL_ARTICLE_DETAIL = "http://sdk.mobcent.com/baikesdk/info/getInfo.do?boardId=11689399&forumKey=9y8jrUJoJQj5vwip4X&topicId=";

	public static void getArticle() {
		MysqlClient client = null;
		try {
			client = MysqlClient.getInstance(jdbcUrl, jdbcName, jdbcPassword);
			String s = HttpTools.get(URL_ROOT);
			System.out.println(new String(s.getBytes("gb2312"),"utf-8"));
			JSONObject jsonObject = JSONObject.parseObject(s);
			JSONArray articleList = jsonObject.getJSONArray("list");
			for (Object o : articleList) {
				JSONObject article = (JSONObject) o;
				JSONObject jsonObject1 = JSONObject.parseObject(HttpTools.get(URL_ARTICLE_DETAIL));
				JSONArray contents = jsonObject1.getJSONArray("content");
				StringBuilder builder = new StringBuilder();
				for (Object o1 : contents) {
					JSONObject content = (JSONObject) o1;
					if ("0".equals(content.get("type"))) {
						builder.append(content.get("infor"));
					}
				}
				article.put("content", builder.toString());
				ArticleBean articleBean = JSONObject.parseObject(article.toString(), ArticleBean.class);
				System.out.println(articleBean);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			client.destroy();
		}

	}
}
